CXXTARGETS = factorial-memoization factorial-tabulation \
          common-sub-string-plain-iterative common-sub-string-plain-recursive \
          common-sub-string-dynamic-programming common-sub-string-dynamic-programming-space-optimized \
          01-knapsack-problem-recursive 01-knapsack-problem-memoization 01-knapsack-problem-tabulation \
          merging-stones-recursive merging-stones-memoization merging-stones-tabulation

CTARGETS=malloc-1 malloc-2 malloc-3 malloc-without-free stack-data static-data-1 static-data-2

CFLAGS = -Wall -Wextra -Werror -O2 -g -std=c99
CXXFLAGS = -Wall -Wextra -Werror -O2 -g -std=c++14

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S), Linux)
    CXXFLAGS += -DLINUX
endif
ifeq ($(UNAME_S), Darwin)
    CXXFLAGS += -DDARWIN
endif

ifeq ($(CXX), g++)
    CXXFLAGS += -fmax-errors=10
else ifeq ($(CXX), clang++)
    CXXFLAGS += -ferror-limit=10
endif

all:$(CXXTARGETS) $(CTARGETS)

$(CXXTARGETS):%:%.o
	$(CXX) -o $@ $<

$(CTARGETS):%:%.o
	$(CC) -o $@ $<

%.o:%.cpp
	$(CXX) -c $(CXXFLAGS) $< -o $@

%.o:%.c
	$(CC) -c $(CFLAGS) $< -o $@

.PHONY: clean

clean:
	rm -f *.o $(CXXTARGETS) $(CTARGETS)
